package pl.edu.pk.rz.service;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import pl.edu.pk.rz.entity.User;
import pl.edu.pk.rz.exception.UserNotFoundException;

@Service
public class UserService extends AbstractService<User> {

	protected UserService() {
		super(User.class);
	}

	@Transactional
	public User login(String username, String password) throws UserNotFoundException {
		Criteria criteria = sf.getCurrentSession().createCriteria(User.class);
		criteria.add(Restrictions.eq(User.FIELD_USERNAME, username));
		criteria.add(Restrictions.eq(User.FIELD_PASSWORD, password));
		Object user = criteria.uniqueResult();
		if(user == null){
			throw new UserNotFoundException();
		}
		return (User) user;
	}

}